%%
%% Task 1
%%

declare Length Take Drop Append Member Position

%Finner lengden av lista
fun {Length Xs}
   case Xs of _|T then
      1 + {Length T}
   else
      0
   end
end

% Henter de N første elementene fra en liste
fun {Take Xs N}
   if N > {Length Xs} then
      Xs
   elseif N > 0 then
      case Xs of H|T then
	 H|{Take T (N-1)}
      else
	 nil
      end
   else
      nil
   end
end

% Henter de elementene som er etter de N første.
fun {Drop Xs N}
   if N>0 then
      case Xs of _|T then
	 {Drop T (N-1)}
      else
	 nil
      end
   else
      Xs
   end
end

% Legger sammen to lister Xs og Ys
fun {Append Xs Ys}
   case Xs of H|T then
      H|{Append T Ys}
   else
      Ys
   end
end

% Sann om en liste Xs inneholder Y
fun {Member Xs Y}
   case Xs of H|T then
      if H == Y then
	 true
      else
	 {Member T Y}
      end
   else
      false
   end
end

% Finner posisjonen til Y i lista Xs.
% Nummerering starter på 1.
fun {Position Xs Y}
   case Xs of H|T then
      if H==Y then
	 1
      else
	 1+{Position T Y}
      end
   else
      0
   end
end

